package com.pktechan.api.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.pktechan.api.base.wxPay.WXPay;
import com.pktechan.api.entity.WxPayOrder;
import com.pktechan.api.enumeration.RefundAccountEnum;
import com.pktechan.api.exception.CustomException;
import com.pktechan.api.service.WxPayOrderService;
import com.pktechan.api.service.WxPayService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: cloudfloating
 * @description: 微信接口实现
 * @author: qx.zhang
 * @create: 2021-03-10 15:26
 **/
@Service("wxPayService")
@Configuration
public class WxPayServiceImpl implements WxPayService {

    private final static Logger logger = LoggerFactory.getLogger(WxPayServiceImpl.class);

    @Value("${wxpay.notify-url}")
    private String wxPayNotifyUrl;

    @Autowired
    private WxPayOrderService wxPayOrderService;

    private WXPayConfigImpl wxPayConfig;
    private WXPay wxPay;

//    public WxPayServiceImpl() throws Exception {
//        wxPayConfig = WXPayConfigImpl.getInstance();
//        wxPay = new WXPay(wxPayConfig);
//    }

    /**
     * 统一下单接口
     *
     * @param reqMap
     * @return
     */
    @Override
    public Map<String, String> doUnifiedOrder(Map<String, String> reqMap) {
        try {
            wxPayConfig = WXPayConfigImpl.getInstance();
            wxPay = new WXPay(wxPayConfig);
            Map<String, String> respMap = wxPay.unifiedOrder(reqMap);
            logger.debug(JSONObject.toJSONString(respMap));
            return respMap;
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException("统一下单接口异常。订单号：" + reqMap.get("out_trade_no"));
        }
    }

    /**
     * 订单查询
     *
     * @param outTradeNo
     * @return
     */
    @Override
    public Map<String, String> doOrderQuery(String outTradeNo) {
        logger.debug("查询微信支付订单，订单号：" + outTradeNo);
        HashMap<String, String> data = new HashMap<String, String>();
        data.put("out_trade_no", outTradeNo);
//        data.put("transaction_id", "4008852001201608221962061594");
        try {
            wxPayConfig = WXPayConfigImpl.getInstance();
            wxPay = new WXPay(wxPayConfig);
            Map<String, String> respMap = wxPay.orderQuery(data);
            logger.debug(JSONObject.toJSONString(respMap));
            return respMap;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("查询微信支付订单异常，订单号：" + outTradeNo);
            throw new CustomException("查询微信支付订单异常，订单号：" + outTradeNo);
        }
    }

    /**
     * 申请退款
     *
     * @param wxPayOrder
     * @return
     */
    @Override
    public Map<String, String> refundWxPayOrder(WxPayOrder wxPayOrder) throws Exception {
        HashMap<String, String> reqData = new HashMap<String, String>();
        reqData.put("out_trade_no", wxPayOrder.getOutTradeNo());
        reqData.put("out_refund_no", wxPayOrderService.createId());
        reqData.put("total_fee", String.valueOf(wxPayOrder.getTotalFee()));
        reqData.put("refund_fee", String.valueOf(wxPayOrder.getTotalFee()));
        //商品已售完	若商户传入，会在下发给用户的退款消息中体现退款原因
        reqData.put("refund_account", RefundAccountEnum.REFUND_SOURCE_RECHARGE_FUNDS.getName());
        reqData.put("refund_desc", "商品已售完");
        reqData.put("notify_url", wxPayNotifyUrl);
        wxPayConfig = WXPayConfigImpl.getInstance();
        wxPay = new WXPay(wxPayConfig);
        //进行签名
        Map<String, String> respMap = wxPay.refund(reqData);
        return respMap;
    }
}
